package com.ui.basic.controller.admin;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.common.basic.base.BaseController;
import com.common.basic.util.Globals;
import com.ui.basic.command.CommandMenu;
import com.ui.basic.service.admin.MenuService;

@Controller
public class SetupMenuController extends BaseController {
	private static final transient org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory
			.getLog(SetupMenuController.class);

	@Autowired
	private MenuService menuService;

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	@RequestMapping(value = "/admin/menu.page", method = RequestMethod.GET)
	public String page(
			final HttpServletRequest request,
			final HttpServletResponse response) {
		LOGGER.debug(">>>SetupMenuController.page()");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		return "menu.page";
	}

	@RequestMapping(value = "/admin/menu.view", method = RequestMethod.POST)
	public String view(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandMenu") CommandMenu commandMenu) {
		LOGGER.debug(">>>SetupMenuController.view");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		try {
			model.addAttribute("commandUser", menuService.getMenu(commandMenu));
		} catch (SQLException e) {
			LOGGER.error(">>>Error :: " + e.getMessage());
			sendError(response, e.getMessage());
			return null;
		}
		return "menu.view";
	}

	@RequestMapping(value = "/admin/menu.form", method = RequestMethod.POST)
	public String edit1(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandMenu") CommandMenu commandMenu) {
		LOGGER.debug(">>>SetupMenuController.edit1");
		return edit(model, request, response, commandMenu);
	}
	
	@RequestMapping(value = "/admin/menu.edit", method = RequestMethod.POST)
	public String edit(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandMenu") CommandMenu commandMenu) {
		LOGGER.debug(">>>SetupMenuController.edit");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		try {
			if (commandMenu.getId() != null)
				model.addAttribute("commandMenu", menuService
						.getUserDetail(commandMenu));
			else {
				model.addAttribute("commandMenu", commandMenu);
				commandMenu.setStrAction(Globals.ACTION_ADD);
			}
		} catch (SQLException e) {
			LOGGER.error(">>>Error :: " + e.getMessage());
			sendError(response, e.getMessage());
			return null;
		}

		return "menu.edit";
	}

	@RequestMapping(value = "/admin/menu.save", method = RequestMethod.POST)
	public String save(HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandMenu") CommandMenu commandMenu,
			BindingResult result, Model model) {
		LOGGER.debug(">>>SetupMenuController.save");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		validator.validate(commandMenu, result);

		if (!hasFieldErrors(request, response, result)) {
			try {
				menuService.save(commandMenu);
				return view(model, request, response, commandMenu);
			} catch (Exception e) {
				if (commandMenu.getId() != null)
					sendError(response, getMessage(request,
							"error.menudata.saveFailed",
							new String[] { commandMenu.getCode() + " - "
									+ commandMenu.getName() }));
				else
					sendError(response, getMessage(request,
							"error.menudata.exist", new String[] { commandMenu
									.getCode()
									+ " - " + commandMenu.getName() }));
				return null;
			}
		}
		return null;
	}

	@RequestMapping(value = "/admin/menu.del", method = RequestMethod.POST)
	public String delete(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandMenu") CommandMenu commandMenu,
			BindingResult result) {
		LOGGER.debug(">>>SetupMenuController.delete");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		if (commandMenu.getId() != null
				|| (commandMenu.getSelectedRecord() != null && commandMenu
						.getSelectedRecord().length > 0)) {
			try {
				menuService.delete(commandMenu);
				return view(model, request, response, commandMenu);
			} catch (Exception e) {
				sendError(response, e.getMessage());
				return null;
			}
		}
		sendError(response, getMessage(request, "label.noDataSelected"));
		return null;
	}
}
